package com.itboye.sunsun.personsetting.ui;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.sunsunhome.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.luntan.LunTanPinDaoActivity;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.person.ui.PersonSettingActivity;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

public class SetNickNameActivity extends BaseActivity implements
		OnClickListener {

	private Button confirm;
	private EditText edittext;
	private RelativeLayout backrela;
	private ImageView back;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_nickname);
		String uri = (String) SPUtils.get(App.ctx, null, SPContants.NICK, "");
		edittext.setHint(uri);
		setStatusBarColor(R.color.home_blue);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.confirm) {
			if (!edittext.getText().toString().equals(null)) {
				setNickName();
			} else {
				Toast.makeText(App.ctx, "输入错误", 0).show();
			}

		} else if (v.getId() == R.id.back) {
			this.finish();
		}

	}

	public void setNickName() {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100")
				.typeKey("BY_User_update")
				.param("uid",
						(String) SPUtils.get(App.ctx, null, SPContants.USER_ID,
								""))
				.param("nickname", edittext.getText().toString())
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						// Toast.makeText(App.ctx, "修改成功", 0).show();
						SPUtils.put(App.ctx, null, SPContants.NICK, edittext
								.getText().toString());

						
						Intent intent2 = new Intent("necheng");
						sendBroadcast(intent2);
						
						Intent intent = new Intent(SetNickNameActivity.this,
								PersonSettingActivity.class);
						intent.putExtra("nickname", edittext.getText()
								.toString());
						setResult(RESULT_OK, intent);
						SetNickNameActivity.this.finish();
						
						
						
						
						
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						} else {
							Toast.makeText(App.ctx, "网络异常", 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);

	}
	
	
	

}
